package com.llsydn.demo.motion;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.llsydn.hello.R;

/**
 * @author lilinshen
 * @title MotionEvent拖动View，连续2次Back退出应用
 * @description 请填写相关描述
 * @date 2018/9/28 16:05
 */
public class MotionActivity extends AppCompatActivity implements View.OnTouchListener {

    private ImageView iv_main;
    private RelativeLayout parentView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_motion);
        iv_main = findViewById(R.id.iv_main);
        parentView = (RelativeLayout) iv_main.getParent();

        iv_main.setOnTouchListener(this);
    }

    private int lastX;
    private int lastY;
    private int maxRight;
    private int maxBottom;

    /**
     * 拖动view例子
     *
     * @param v
     * @param event
     * @return
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int eventX = (int) event.getRawX(); //获取相对屏幕左顶点的x轴位置
        int eventY = (int) event.getRawY(); //获取相对屏幕左顶点的y轴位置
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //第一次记录lastX、lastY
                lastX = eventX;
                lastY = eventY;
                //得到父视图的right、bottom（只需执行一次）
                if (maxRight == 0) {
                    maxRight = parentView.getRight();
                    maxBottom = parentView.getBottom();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                //计算事件的偏移
                int dx = eventX - lastX;
                int dy = eventY - lastY;
                //根据事件的偏移来移动imageview
                int left = iv_main.getLeft() + dx;
                int top = iv_main.getTop() + dy;
                int right = iv_main.getRight() + dx;
                int bottom = iv_main.getBottom() + dy;

                //限制不能拖动到屏幕外面
                if (left < 0) {
                    right += -left; //right要变大
                    left = 0;
                }
                if (top < 0) {
                    bottom += -top;
                    top = 0;
                }
                //限制right <=maxRight
                if (right > maxRight) {
                    left -= right - maxRight;
                    right = maxRight;
                }
                //限制bottom <=maxBottom
                if (bottom > maxBottom) {
                    top -= bottom - maxBottom;
                    bottom = maxBottom;
                }

                iv_main.layout(left, top, right, bottom);
                //再次记录lastX、lastY
                lastX = eventX;
                lastY = eventY;
                break;
            default:
                break;
        }
        return true;//所有的motionEvent都交给imageView处理
    }


    private boolean exit = false;//标识是否可以退出
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                exit = false;
            }
        }
    };

    /**
     * 2s内有效，按退出键退出应用
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (!exit) {
                exit = true;
                Toast.makeText(MotionActivity.this, "再按一次退出应用(2s内)", Toast.LENGTH_SHORT).show();
                //发消息延迟2s，将exit设置为false
                handler.sendEmptyMessageDelayed(1, 2000);
                return true;
            }
        }
        return super.onKeyUp(keyCode, event);
    }
}
